package com.cloudcc.mobile.view.market;

import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.bus.IEventLife;
import com.cloudcc.cloudframe.ui.viewgroup.loadmore.LoadMoreContainer;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrFrameLayout;
import com.cloudcc.cloudframe.util.ListUtils;
import com.cloudcc.mobile.ExtraConstant;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.adapter.MarketActivityListAdapter;
import com.cloudcc.mobile.entity.market.MarketActivityEntity;
import com.cloudcc.mobile.event.MarketActivityEventList.MarketEventListEvent;
import com.cloudcc.mobile.presenter.MarketActivityPresenter;
import com.cloudcc.mobile.view.main.fragment.BaseListFragment;
import com.cloudcc.mobile.widget.CloudCCTitleBar;
import com.cloudcc.mobile.widget.CloudCCTitleBar.OnTitleBarClickListener;
import com.cloudcc.mobile.widget.listview.CloudCCListView;
import com.cloudcc.mobile.widget.listview.CloudCCListView.OnRefreshOrLoadMoreListener;

/**
 * 市场活动列表
 * 
 * @Description: TODO()
 * @author wangzhiqiang
 * @date 2016-3-15 下午2:50:00
 * 
 */
public class MarketActivityListFragment extends BaseListFragment implements
		IEventLife, OnRefreshOrLoadMoreListener, OnItemClickListener,
		OnTitleBarClickListener {

	@Bind(R.id.listview1)
	CloudCCListView mListView;

	@Bind(R.id.et_search_content)
	EditText mSearchContent;

	@Bind(R.id.ll_searchparent)
	LinearLayout ll_searchparent;

	@Bind(R.id.headerbar)
	protected CloudCCTitleBar headerbar;

	private MarketActivityPresenter mMarketPresenter = new MarketActivityPresenter();

	private MarketActivityListAdapter mAdapter;

	@Override
	public void init() {
		register();
		initHeader();
		mListView.setOnRefreshOrLoadMoreListener(this);
		mListView.requestRefresh();
		initEditText();
	}

	private void initHeader() {
		headerbar.setOnTitleBarClickListener(this);
		headerbar.setTitle("市场活动");
	}

	private void initEditText() {
		mSearchContent
				.setOnEditorActionListener(new EditText.OnEditorActionListener() {

					@Override
					public boolean onEditorAction(TextView v, int actionId,
							KeyEvent event) {
						if (actionId == EditorInfo.IME_ACTION_SEARCH) {
							clickSearch();
							return true;
						}
						return false;
					}

				});
	}

	@OnClick(R.id.search)
	public void clickSearch() {
		InputMethodManager imm = (InputMethodManager) mContext
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(mSearchContent.getWindowToken(), 0);
		mListView.requestRefresh();
	}

	@Override
	public void requestData() {
		headerbar.beginProgress();
		mMarketPresenter.queryMarketActivityList(currentPage, mSearchContent
				.getText().toString());
	}

	/**
	 * 加载到数据
	 * 
	 * @param event
	 */
	public void onEventMainThread(MarketEventListEvent event) {
		headerbar.completeProgress();
		boolean shoudClear = mListView.refreshLoadMoreUI(
				ListUtils.isEmpty(event.getData()), event.isError(),
				isFromRefresh(), event.getMessage());
		if (mAdapter == null) {
			mAdapter = new MarketActivityListAdapter(mContext);
			mListView.setAdapter(mAdapter);
			mListView.setOnItemClickListener(this);
		}
		if (shoudClear) {
			mAdapter.changeData(event.getData());
		} else {
			mAdapter.addData(event.getData());
		}
	}

	@Override
	public void register() {
		EventEngine.register(this);
	}

	@Override
	public void unRegister() {
		EventEngine.uregister(this);
	}

	@Override
	public void onDestroy() {
		unRegister();
		super.onDestroy();
	}

	/**
	 * 进入详情页面
	 * 
	 * @param t
	 * @param siEdit
	 */
	private void enterInfo(MarketActivityEntity t) {
		Intent intent = new Intent(mContext, MarketActivityInfoActivity.class);
		intent.putExtra(ExtraConstant.EXTRA_NAME, t.getName());
		intent.putExtra(ExtraConstant.EXTRA_ID, t.getId());
		startActivity(intent);
	}

	@Override
	public void onRefresh(PtrFrameLayout frame) {
		onRefreshData();
	}

	@Override
	public void onLoadMore(LoadMoreContainer frame) {
		onLoadMoreData();
	}

	@Override
	public int getLayoutId() {
		return R.layout.marketactivity_list_frag;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		MarketActivityEntity t = mAdapter.getItem(position);
		enterInfo(t);
	}

	@Override
	public void onClickLeft(View v) {
		getActivity().finish();
	}

	@Override
	public void onClickRight(View v) {

	}
}
